package com.yjf.ljr.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.dataclass.PayPwdSettingDataClass;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

public class PaymentActivity extends IjActivity implements View.OnClickListener {

	@ID("accountStatusContainer")
	private RelativeLayout accountStatusContainer;// 支付账户状态栏
	@ID("tvaccountState")
	private TextView tvAccountState;// 支付账户状态
	@ID("realName")
	private TextView tvRealName;// 支付账户
	@ID("ivacountState")
	private ImageView ivAcountState;//支付账户状态图标
	
	@ID("changePaymentPwdContainer")
	private RelativeLayout changePaymentPwdContainer;
	
	private String paymentStatus;
	private String realName;
	
	private String mobile;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_payment);
		initControl();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("支付账户");
		paymentStatus = getIntent().getStringExtra("paymentStatus");
		realName = getIntent().getStringExtra("realName");
		String openStatus = getString(R.string.payment_status_on);
		String activeStatus = getString(R.string.payment_status_active);
		mobile = getIntent().getStringExtra("mobile");
		
		if(!TextUtils.isEmpty(paymentStatus) && (paymentStatus.equals(openStatus) || paymentStatus.equals(activeStatus))) {
			openStatus = getString(R.string.payment_status_on);
			accountStatusContainer.setClickable(false);
			accountStatusContainer.setEnabled(false);
			ivAcountState.setImageResource(R.drawable.certified_status);
			changePaymentPwdContainer.setVisibility(View.VISIBLE);
		} else {
			openStatus = getString(R.string.payment_status_off);
			accountStatusContainer.setClickable(true);
			accountStatusContainer.setEnabled(true);
			ivAcountState.setImageResource(R.drawable.arrow_right);
			changePaymentPwdContainer.setVisibility(View.GONE);
		}	
		if(TextUtils.isEmpty(paymentStatus)) {
			tvAccountState.setText(openStatus);
		} else {
			tvAccountState.setText(paymentStatus);
		}
		
        if(!TextUtils.isEmpty(realName)) {
			tvRealName.setText(realName);
		}
				
		accountStatusContainer.setOnClickListener(this);
		changePaymentPwdContainer.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		//激活账户
		case R.id.accountStatusContainer:	
			Intent intent = new Intent(PaymentActivity.this,RegistrationActiveActivity.class);
			intent.putExtra("phoneNum", mobile);
			startActivityForResult(intent, CommonData.REQUEST_CODE_LOGIN);
			break;
		case R.id.changePaymentPwdContainer:
			new PayPwdSettingTask().execute();
			break;
		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case CommonData.REQUEST_CODE_LOGIN:
				setResult(RESULT_OK);
				finish();
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}
	
	
	
	class PayPwdSettingTask extends AsyncTask<Void, Void, String> {
		private PayPwdSettingDataClass dc = new PayPwdSettingDataClass();
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showProgressDialog();
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject mObject = new RequestObject();
			mObject.method = "changePayPwd";
			return requestDataAndGetErrorMsg(mObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			dismissProgressDialog();
			if ("-1".equals(dc.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(result)) {
				showToast(result);				
			} else {			
                String url = dc.modifyPayPwdUrl;
                if(!TextUtils.isEmpty(url)) {
                	Intent intent = new Intent(PaymentActivity.this,WvUpdatePwdActivity.class);
    				intent.putExtra("advUrl", url);
    				intent.putExtra("advTitle", "重置支付密码");
    				intent.putExtra("returnUrl", "userManage/accountSetting");
    				startActivity(intent);	
    			}
			}
		}
	}

}
